from bottle import route, run, request, response
from matcher import *
from webservice_defs import *
import atexit

@route('/likes')
def is_target_suitable():
	sourceid = request.query.sourceid
	targetid = request.query.targetid
	
	print "is_target_suitable - sourceid: "+str(sourceid)+" targetid: "+str(targetid)

	try:
		r = matcher.is_target_suitable(sourceid, targetid)
	except Exception, err:
		print {'success' : False, 'reason' : str(err)} 
		return {'success' : False, 'reason' : str(err)} 

	print {'success' : True, 'result' : {'suitable' : r[1], 'value' : r[0]}}		
	return {'success' : True, 'result' : {'suitable' : r[1], 'value' : r[0]}}
		
@route('/likes/users')
def get_valid_targets():
	userid = request.query.sourceid
	slimit = request.query.limit

	print "get_valid_targets - userid: "+str(userid)+" slimit: "+str(slimit)
	
	if not slimit or int(slimit) > MAX_LIMIT or int(slimit) <= 0:
		limit = DEFAULT_LIMIT
	else:
		limit = int(slimit)
	
	try:
		results = matcher.get_valid_targets(userid, limit)
		r = []
		for t in results:
			r.append({'targetid' : t[0], 'value' : t[1]})
	except Exception, err:
		print {'succcess' : False, 'reason' : str(err)}
		return {'succcess' : False, 'reason' : str(err)}

	print {'success' : True, 'result' : r}		
	return {'success' : True, 'result' : r}

@route('/users', method='POST')
def create_user():
	userid  = request.forms.get('userid')
	genderid = request.forms.get('genderid')
	interestedin = request.forms.get('interestedin')

	print "create_user - userid: "+str(userid)+" genderid: "+str(genderid)+" interestedin: "+str(interestedin)
	
	if not genderid in SEXES:
		print {'success' : False, 'reason' : 'Invalid gender id'} 
		return {'success' : False, 'reason' : 'Invalid gender id'} 
		
	if not interestedin in SEXES:
		print {'success' : False, 'reason' : 'Invalid interestedin'} 
		return {'success' : False, 'reason' : 'Invalid interestedin'} 
	
	try:
		matcher.create_user(userid, genderid, interestedin)
		response.status = 201
	except Exception, err:
		print {'success' : False, 'reason' : str(err)} 
		return {'success' : False, 'reason' : str(err)} 

	print {'success' : True} 		
	return {'success' : True} 
	
@route('/users', method='DELETE')
def delete_user():
	userid = request.query.userid

	print "delete_user - userid: "+str(userid)
	
	try:
		matcher.delete_user(userid)
	except Exception, err:
		print {'success' : False, 'reason' : str(err)}
		return {'success' : False, 'reason' : str(err)}

	print {'success' : True}		
	return {'success' : True}
	
@route('/users/images', method='POST')
def update_user_image():
	userid = request.forms.get('userid')
	data = request.files.data
	
	print "update_user_image - userid: "+str(userid)

	try:
		image = data.file.read()
		print "Uploaded image size:", len(image)
		matcher.update_user_image(userid, image)
	except Exception, err:
		print {'success' : False, 'reason' : str(err)} 
		return {'success' : False, 'reason' : str(err)} 

	print {'success' : True}		
	return {'success' : True}
	
@route('/users/training', method='PUT')
def train_user_bot():
	userid = request.forms.get('userid')
	training_imageid = request.forms.get('trainingid')
	sresponse = request.forms.get('response')

	print "train_user_bot - userid: "+str(userid)+" training_imageid: "+str(training_imageid)+" sresponse: "+str(sresponse)
	
	if not sresponse or float(sresponse) > MAX_RESPONSE \
	   or float(sresponse) < MIN_RESPONSE:
		return {'success' : False, 'reason' : 'Invalid response'} 
		
	user_response = float(sresponse)
	
	try:
		matcher.train_user_bot(userid, training_imageid, user_response)
	except Exception, err:
		print {'success' : False, 'reason' : str(err)} 
		return {'success' : False, 'reason' : str(err)} 

	print {'success' : True} 		
	return {'success' : True} 
	
@route('/users/method', method='PUT')
def set_method():
	userid = request.forms.get('userid')
	methodid = request.forms.get('methodid')

	print "set_method - userid: "+str(userid)+" methodid: "+str(methodid)
	
	if not methodid in METHODS:
		print {'success' : False, 'reason' : 'Invalid method id'} 
		return {'success' : False, 'reason' : 'Invalid method id'} 
	
	try:
		matcher.set_method(userid, methodid)
	except Exception, err:
		print {'success' : False, 'reason' : str(err)} 
		return {'success' : False, 'reason' : str(err)} 

	print {'success' : True}		
	return {'success' : True}
	
@route('/users/interestedin', method='PUT')
def set_interestedin():
	userid = request.forms.get('userid')
	interestedin = request.forms.get('interest')
	
	print "set_interestedin - userid: "+str(userid)+" interestedin: "+str(interestedin)

	if not interestedin in SEXES:
		print {'success' : False, 'reason' : 'Invalid interestedin'} 
		return {'success' : False, 'reason' : 'Invalid interestedin'} 
	
	try:
		matcher.set_interestedin(userid, interestedin)
	except Exception, err:
		print {'success' : False, 'reason' : str(err)} 
		return {'success' : False, 'reason' : str(err)} 

	print {'success' : True} 		
	return {'success' : True} 
	
@route('/users/gender', method='PUT')
def set_gender():
	userid = request.forms.get('userid')
	genderid = request.forms.get('genderid')

	print "set_gender - userid: "+str(userid)+" genderid: "+str(genderid)
	
	if not genderid in SEXES:
		print {'success' : False, 'reason' : 'Invalid gender id'} 
		return {'success' : False, 'reason' : 'Invalid gender id'} 
	
	try:
		matcher.set_gender(userid, genderid)
	except Exception, err:
		print {'success' : False, 'reason' : str(err)} 
		return {'success' : False, 'reason' : str(err)} 

	print {'success' : True} 		
	return {'success' : True} 
	
def exit():
	matcher.save()
	
matcher = Matcher()
atexit.register(exit)
run(host='localhost', port=7070)

